(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:ServiceactivityCtrl
     * @description
     * # ServiceactivityCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('ServiceactivityCtrl', ServiceactivityCtrl);

    ServiceactivityCtrl.$inject = ['$scope', 'dataService', 'helper', '$rootScope', '$location', 'sessionService'];

    function ServiceactivityCtrl($scope, dataService, helper, $rootScope, $location, sessionService) {
        //服务活动列表
        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getList(1);
            $scope.$on('event:up-store-id', function() {
                $scope.getList(1);
            });
        }

        $scope.getList = function(pageNo) {
            var query = {
                content: {
                    page_no: pageNo,
                    page_size: $scope.page_Size ? $scope.page_Size : 10,
                }
            };

            dataService.serviceactivityGet(query).then(function(list) {
                if (list) {
                    $scope.fuwu_list = list.fuwu_list;
                    $scope.total_count = list.total_count;
                }
            });
        }

        //添加页面
        $scope.addAction = function() {
            $scope.fuwu_activity_id = $location.search().fuwu_activity_id;

            if ($scope.fuwu_activity_id) {
                getOneData();
                $scope.$on('event:up-store-id', function() {
                    getOneData();
                });
            } else {
                $scope.activity_data = {
                    "name": "报名活动",
                    "img": "",
                    "img1": "",
                    "img2": "",
                    "img3": "",
                    "img4": "",
                    "start_time": $scope.getDateStr(0),
                    "end_time": $scope.getDateStr(7),
                    "soft_version": 1,
                    "pay_amount": 100,
                    "original_price": 200,
                    "introduce": "活动说明",
                    "remark": "",
                };
            }
        }

        //活动添加
        $scope.activityAdd = function() {
            if ($scope.activity_data.end_time < $scope.activity_data.start_time) {
                helper.myAlert("结束时间不能小于开始时间");
                return false;
            }

            if (!$scope.activity_data.original_price) {
                helper.myAlert("原价不能为空！");
                return false;
            }

            if ($scope.activity_data.pay_amount > $scope.activity_data.original_price) {
                helper.myAlert("服务费用必须小于原价");
                return false;
            }

            if ($scope.activity_data.pay_amount <= 0 || !$scope.activity_data.pay_amount) {
                helper.myAlert("服务费不能为空！不能为负值！");
                return false;
            }

            if ($scope.fuwu_activity_id) {
                dataService.serviceactivityUpdate($scope.activity_data).then(function(result) {
                    if (result.fuwu_activity_id) {
                        helper.myAlert("success", "#/serviceactivity/list");
                    } else {
                        helper.myAlert(result.errmsg);
                    }
                });
            } else {
                dataService.serviceactivityAdd($scope.activity_data).then(function(result) {
                    if (result.fuwu_activity_id) {
                        helper.myAlert("success", "#/serviceactivity/list");
                    } else {
                        helper.myAlert(result.errmsg);
                    }
                });
            }
        }

        //单个服务活动数据
        function getOneData() {
            var query = {
                content: {
                    fuwu_activity_id: $scope.fuwu_activity_id
                }
            };

            dataService.serviceactivityGet(query).then(function(result) {
                if (result.fuwu_list) {
                    $scope.activity_data = result.huodong_list[0];
                    $scope.activity_data.introduce = json2html(JSON.parse($scope.activity_data.introduce));
                }
            });
        }

        //活动时间计算
        $scope.getDateStr = function(addDayCount) {
            var dd = new Date();
            dd.setDate(dd.getDate() + addDayCount);
            var y = dd.getFullYear();
            var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1);
            var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
            var h = dd.getHours();
            var i = dd.getMinutes();
            var s = dd.getSeconds();

            if (i < 10) {
                i = "0" + i;
            }
            var s = dd.getSeconds();
            if (s < 10) {
                s = "0" + s;
            }
            return y + "-" + m + "-" + d + " " + h + ":" + i + ":" + s;
        }


        //报名活动详情列表
        $scope.detailAction = function() {
            $scope.pageSize = 10;
            $scope.huodong_id = $location.search().huodong_id;
            $scope.chosedStatus = "all";

            $scope.getDetail1(1);
            getOneData();
        }

        $scope.getDetail1 = function(pageNo) {
            var query = {
                content: {
                    page_no: pageNo,
                    page_size: $scope.page_Size ? $scope.page_Size : 10,
                    store_id: $rootScope.store_id,
                    huodong_id: $scope.huodong_id,
                }
            };

            if ($scope.chosedStatus && $scope.chosedStatus != "all") {
                if ($scope.chosedStatus == "weizhifu") {
                    query.content.status = 1;
                }
                if ($scope.chosedStatus == "yizhifu") {
                    query.content.status = 2;
                }
                if ($scope.chosedStatus == "yiqiandao") {
                    query.content.status = 3;
                }
                if ($scope.chosedStatus == "作废") {
                    query.content.status = 10;
                }
            }

            dataService.huodongDetailGet(query).then(function(list) {
                if (list) {
                    $scope.detail_list = list.detail_list;
                    $scope.detail_count11 = list.detail_count;
                }
            });
        }
    }
})();